package com.dennytech.tac.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dennytech.tac.net.BaseApi;

import android.os.Parcel;
import android.os.Parcelable;

public class Attraction extends CFObject implements Parcelable {

	int id;
	int type;// 类型（文化景观、公园、建筑、野外）
	int hot;
	String name;
	String intro;
	String detail;
	String photoUrl;
	String sPhotoUrl;
	String tel;
	String address;
	double latitude;
	double longitude;
	String opentime;
	String price;

	public Attraction() {
	}

	public Attraction(Parcel in) {
		id = in.readInt();
		type = in.readInt();
		hot = in.readInt();
		name = in.readString();
		intro = in.readString();
		detail = in.readString();
		photoUrl = in.readString();
		sPhotoUrl = in.readString();
		tel = in.readString();
		address = in.readString();
		latitude = in.readDouble();
		longitude = in.readDouble();
		opentime = in.readString();
		price = in.readString();
	}

	public static Attraction decode(JSONObject source) {
		Attraction attraction = null;
		try {
			attraction = new Attraction();
			attraction.setId(source.getInt("id"));
			attraction.setName(source.getString("name"));
			attraction.setIntro(source.getString("intro"));
			attraction.setDetail(source.getString("detail"));

			String photoUrl = source.getString("photourl");
			if (!"".equals(photoUrl)) {
				attraction.setPhotoUrl(BaseApi.API_DOMAIN
						+ source.getString("photourl"));

				String[] temp = photoUrl.split("\\.");
				if (temp.length == 2) {
					attraction.setSPhotoUrl(BaseApi.API_DOMAIN + temp[0]
							+ "_s." + temp[1]);
				}
			}

			if (!"".equals(source.getString("atype"))) {
				attraction.setType(Integer.valueOf(source.getString("atype")));
			}

			if (!"".equals(source.getString("hot"))) {
				attraction.setHot(Integer.valueOf(source.getString("hot")));
			}

			attraction.setTel(source.getString("tel"));
			attraction.setAddress(source.getString("address"));

			if (!"".equals(source.getString("latitude"))) {
				attraction.setLatitude(Double.valueOf(source
						.getString("latitude")));
			}

			if (!"".equals(source.getString("longitude"))) {
				attraction.setLongitude(Double.valueOf(source
						.getString("longitude")));
			}
			attraction.setOpentime(source.getString("opentime"));
			attraction.setPrice(source.getString("price"));
		} catch (Exception e) {
			return null;
		}

		return attraction;
	}

	public static List<Attraction> decodeList(String source) {
		List<Attraction> list = new ArrayList<Attraction>();
		try {
			JSONArray listObj = new JSONObject(source).getJSONArray("list");
			for (int i = 0; i < listObj.length(); i++) {
				JSONObject o = listObj.getJSONObject(i);
				Attraction a = decode(o);
				list.add(a);
			}

		} catch (JSONException e) {
			return null;
		}
		return list;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getHot() {
		return hot;
	}

	public void setHot(int hot) {
		this.hot = hot;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getPhotoUrl() {
		return photoUrl;
	}

	public void setPhotoUrl(String photoUrl) {
		this.photoUrl = photoUrl;
	}

	public String getSPhotoUrl() {
		return sPhotoUrl;
	}

	public void setSPhotoUrl(String sPhotoUrl) {
		this.sPhotoUrl = sPhotoUrl;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public String getOpentime() {
		return opentime;
	}

	public void setOpentime(String opentime) {
		this.opentime = opentime;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(type);
		dest.writeInt(hot);
		dest.writeString(name);
		dest.writeString(intro);
		dest.writeString(detail);
		dest.writeString(photoUrl);
		dest.writeString(sPhotoUrl);
		dest.writeString(tel);
		dest.writeString(address);
		dest.writeDouble(latitude);
		dest.writeDouble(longitude);
		dest.writeString(opentime);
		dest.writeString(price);
	}

	public static final Parcelable.Creator<Attraction> CREATOR = new Parcelable.Creator<Attraction>() {
		public Attraction createFromParcel(Parcel in) {
			return new Attraction(in);
		}

		public Attraction[] newArray(int size) {
			return new Attraction[size];
		}
	};

}
